import { user } from '@/api/user'
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import { useUserStoreWithOut } from '@/store/modules/user'
import { useTagCacheStoreWithOut } from '@/store/modules/tag'
import { useKeepAliveStoreWithOut } from '@/store/modules/keepAlive'
import { resetRouter } from '@/router/utils/routeOperate'
import router from '@/router'
const useSignOut = () => {
  const tagCacheStore = useTagCacheStoreWithOut()

  const appStore = useAppStoreWithOut()
  const { systemConfig } = storeToRefs(appStore)
  const userStore = useUserStoreWithOut()
  const keepAliveStore = useKeepAliveStoreWithOut()
  const signOut = () => {
    user.logout(null, { config: { mockEnable: true } }).then(() => {
      userStore.removeUserInfoCache()
      tagCacheStore.removeAllTagCache()
      tagCacheStore.removeFixedTagCache()
      keepAliveStore.removeAllKeepAlive()
      systemConfig.value.isLogin = false
      appStore.setSystemConfigCache()
      resetRouter()
      nextTick(() => {
        if (!router.currentRoute.value.fullPath.includes('/Login?redirect=')) {
          router.push(`/Login?redirect=${router.currentRoute.value.fullPath}`)
        }
      })
      // setTimeout(() => {
      //   location.reload()
      // }, 10)
    })
  }

  return {
    signOut
  }
}
export default useSignOut
